﻿Imports Ejercicio.EjercicioDataSet
Imports Validacion

Public Class FrmPelicula

    Private Sub FrmPelicula_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ModoBusqueda()
        Me.GeneroTableAdapter.Fill(Me.EjercicioDataSet.Genero)
        Me.PeliculaTableAdapter.Fill(Me.EjercicioDataSet.Pelicula)
        'LlenarComboGenerasBusquedaConGenerosYUnValorDeTodos()
    End Sub

    'Si queremos que el combo de generos en la pantalla de busqueda tenga un valor Todos
    'que identifique que no queremos buscar por un género en particular
    'Se simplificó con un check, pero se deja el código x si quieren verlo...
    'Private Sub LlenarComboGenerasBusquedaConGenerosYUnValorDeTodos()
    '    Dim BusquedaDeGenerosTable As New Ejercicio.EjercicioDataSet.GeneroDataTable()
    '    Me.GeneroTableAdapter.Fill(BusquedaDeGenerosTable)
    '    Dim BusquedaGenerosBindingSource As New BindingSource()
    '    BusquedaGenerosBindingSource.DataSource = BusquedaDeGenerosTable
    '    BusquedaGenerosBindingSource.Sort = "id"
    '    Dim filaGeneroVacio As GeneroRow = BusquedaDeGenerosTable.NewGeneroRow()
    '    filaGeneroVacio.id = -1
    '    filaGeneroVacio.nombre = "Todos"
    '    BusquedaDeGenerosTable.AddGeneroRow(filaGeneroVacio)
    '    cbBusquedaGeneros.DataSource = BusquedaDeGenerosTable
    '    cbBusquedaGeneros.ValueMember = "id"
    '    cbBusquedaGeneros.DisplayMember = "nombre"
    'End Sub

    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        TableAdapterManager.UpdateAll(EjercicioDataSet)
        Me.Close()
    End Sub

    Private Sub btnCancelarYSalir_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelarYSalir.Click
        Me.Close()
    End Sub

    Private Sub FillComboBusquedaGeneros()
        Dim posicion As Integer = 0
        GeneroBindingSource.MoveFirst()
    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        Dim filters As New List(Of String)
        If (Not String.IsNullOrEmpty(txtBusquedaCodigoDeBarras.Text)) Then
            filters.Add("codigo_barras LIKE '%" & txtBusquedaCodigoDeBarras.Text & "%'")
        End If
        If (Not String.IsNullOrEmpty(txtBusquedaNombre.Text)) Then
            filters.Add("nombre LIKE '%" & txtBusquedaNombre.Text & "%'")
        End If
        If (checkBusquedaComboGeneros.Checked) Then
            filters.Add("genero_id = " & CInt(cbBusquedaGeneros.SelectedValue))
        End If
        If (checkBusquedaStock.Checked) Then
            filters.Add("stock = " & numBusquedaStock.Value)
        End If
        If (filters.Count > 0) Then
            PeliculaBindingSource.Filter = filters(0)
            For i As Integer = 1 To filters.Count - 1
                PeliculaBindingSource.Filter += " and " & filters(i)
            Next
        Else
            PeliculaBindingSource.Filter = ""
        End If
    End Sub

    Private Sub checkBusquedaStock_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles checkBusquedaStock.CheckedChanged
        lblBusquedaStock.Enabled = checkBusquedaStock.Checked
        numBusquedaStock.Enabled = checkBusquedaStock.Checked
    End Sub

    Private Sub btnAgregar_Click(sender As System.Object, e As System.EventArgs) Handles btnAgregar.Click
        PeliculaBindingSource.AddNew()
        ResetearCampos()
        ModoEdicion()
    End Sub

    Private Sub ResetearCampos()
        txtCodigoDeBarras.Text = ""
        numStock.Value = 0
    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click
        If (MessageBox.Show("¿Está seguro que desea eliminar esta película?", "Eliminar película", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
            PeliculaBindingSource.RemoveCurrent()
        End If
    End Sub

    Private Sub btnModificar_Click(sender As System.Object, e As System.EventArgs) Handles btnModificar.Click
        ModoEdicion()
    End Sub

    Private Sub ModoEdicion()
        container.Panel1.Enabled = False
        container.Panel2.Enabled = True
    End Sub

    Private Sub ModoBusqueda()
        container.Panel1.Enabled = True
        container.Panel2.Enabled = False
    End Sub

    Private Sub checkBusquedaComboGeneros_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles checkBusquedaComboGeneros.CheckedChanged
        lblBusquedaGenero.Enabled = checkBusquedaComboGeneros.Checked
        cbBusquedaGeneros.Enabled = checkBusquedaComboGeneros.Checked
    End Sub

    Private Sub btnAceptar_Click(sender As System.Object, e As System.EventArgs) Handles btnAceptar.Click
        If isPeliculaValida() Then
            Me.PeliculaBindingSource.EndEdit()
            PeliculaDataGridView.Refresh()
            ModoBusqueda()
        Else
            MessageBox.Show("Debe corregir los campos", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        PeliculaBindingSource.CancelEdit()
        PeliculaDataGridView.Refresh()
        ModoBusqueda()
    End Sub

    Private Function isCodigoDeBarrasValido() As Boolean
        Return Validador.IsOnlyText(txtCodigoDeBarras.Text)
    End Function

    Private Function isNombreValido() As Boolean
        Return Validador.IsOnlyText(txtNombre.Text)
    End Function

    Private Function isDescripcionValida() As Boolean
        Return Validador.IsOnlyText(txtDescripcion.Text)
    End Function

    Private Function isGeneroValido() As Boolean
        Return cbGeneros.SelectedIndex <> -1
    End Function

    Private Function isPeliculaValida() As Boolean
        Return isCodigoDeBarrasValido() AndAlso isNombreValido() AndAlso isDescripcionValida() AndAlso isGeneroValido()
    End Function

    Private Sub txtTexto_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles txtNombre.Validating, txtDescripcion.Validating, txtCodigoDeBarras.Validating
        Dim control As Control = CType(sender, Control)
        If Validador.IsOnlyText(control.Text) Then
            ErrorProvider.SetError(control, "")
        Else
            ErrorProvider.SetError(control, "Debe tener un valor y debe ser texto")
            e.Cancel = True
        End If
    End Sub

    Private Sub cbGeneros_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbGeneros.SelectedIndexChanged
        If container.Panel2.Enabled = True AndAlso cbGeneros.SelectedIndex = -1 Then
            ErrorProvider.SetError(cbGeneros, "Debe tener un valor y debe ser texto")
        Else
            ErrorProvider.SetError(cbGeneros, "")
        End If
    End Sub
End Class